/*
 * @(#)GSPreparedStatementFactory.java 
 *
 * Project: BUS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   HZ SDC     2005-12-03   first release  
 *   Jenny Li   2005-12-31   modify throw exception 
 *   ------------ ---------- ---------------------------------------------------
 *   
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2005 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 */
package com.hitrust.gs.lib.db;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 通过一个数组传递PreparedStatement的条件参数
 */
public class GSPreparedStatementFactory {

  /**
   * 给定一个PreparedStatement, 通过给定的数组设置参数
   * @param aPreparedStatement  建立的statement
   * @param aValues 查询条件参数数组
   * @exception IllegalArgumentException
   *                   包含一个空值
   * @exception IllegalStateException
   *                   在执行的过程中的其他操作错误
   */
  public static void buildStatement(PreparedStatement aPreparedStatement,
                                    Object[] aValues) throws
      IllegalArgumentException, SQLException {

    //查询条件参数数组是空值（null）, 抛出一个例外.
    if (aValues == null ||
        aPreparedStatement == null) {
      throw new IllegalArgumentException(
          "Cannot pass a null value array into buildStatement().");
    }

    try {

      Object value = null;
      for (int i = 0; i < aValues.length; i++) {
        value = aValues[i];

        if (value != null) {
          if (value instanceof GSNullSQLType) {
            aPreparedStatement.setNull(i + 1, ( (GSNullSQLType) value).getFieldType());
          }
          else {
            aPreparedStatement.setObject(i + 1, value);
          }
        }
        else {
//          String message = "Can't use a null value in a prepared statement.  If you want to persist a null value, use the " +
//              "com.healthdec.data.NullSQLType object to represent this in the prepared statement.";
//          throw new IllegalStateException(message);
            //暂时置空字符
            aPreparedStatement.setObject(i + 1, "");
        }
      }
    }
    catch (SQLException e) {
        throw e;
    }
  }
}
